//
//  UIBarButtonItem + Extension.swift
//  ZJCSina
//
//  Created by 朱建超 on 16/11/13.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

import UIKit

//给barButtonItem添加构造方法
extension UIBarButtonItem{
    
    convenience init(title : String = "" , imageName : String? = nil , target : Any? , action : Selector?) {
        
        self.init()
        
        let button = UIButton()
        
        button.setTitle(title, for: .normal)
        //设置文字颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //设置按钮图片
        if let imageName = imageName {
            
            button.setImage(UIImage(named : imageName), for: .normal)
            button.setImage(UIImage(named : imageName + "_highlighted"), for: .highlighted)
            
        }
        
        //监听按钮的点击事件
        if let action = action {
            button.addTarget(target, action: action, for: .touchUpInside)
        }
        button.sizeToFit()
        
        self.customView = button
    }

}
